﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LP580.CG;

public partial class User_UC2UploadPhoto : System.Web.UI.Page
{
    clsUserAccount ua;
    protected void Page_Load(object sender, EventArgs e)
    {
        ua = clsUserAccount.getCookieUser(true);
        bindContrl();
        if (!IsPostBack)
        {
            bindData();
        }
    }

    private void bindContrl()
    {
                
    }

    private void bindData()
    {
        clsCompanyPhoto cls = new clsCompanyPhoto(ua.UserID, 0);
        if (cls.CompanyPhotoID > 0 && cls.PhotoPath != "")
        {
            imgLogo.ImageUrl = "~/" + cls.PhotoPath;
            lkbtnDelLogo.OnClientClick = "return confirm('删除图片文件。\\n\\n确实要执行吗？')";
        }
        else
        {
            lkbtnDelLogo.Enabled = false;
        }
        cls = new clsCompanyPhoto(ua.UserID, 1);
        if (cls.CompanyPhotoID > 0 && cls.PhotoPath != "")
        {
            imgBanner.ImageUrl = "~/" + cls.PhotoPath;
            lkbtnDelBanner.OnClientClick = "return confirm('删除图片文件。\\n\\n确实要执行吗？')";
        }
        else
        {
            lkbtnDelBanner.Enabled = false;
        }
        cls = new clsCompanyPhoto(ua.UserID, 2);
        if (cls.CompanyPhotoID > 0 && cls.PhotoPath != "")
        {
            imgCompany.ImageUrl = "~/" + cls.PhotoPath;
            lkbtnDelCompany.OnClientClick = "return confirm('删除图片文件。\\n\\n确实要执行吗？')";
        }
        else
        {
            lkbtnDelCompany.Enabled = false;
        }
    }


    protected void lkbtnDelLogo_Click(object sender, EventArgs e)
    {
        delImg(0);
    }

    protected void lkbtnDelBanner_Click(object sender, EventArgs e)
    {
        delImg(1);
    }

    protected void lkbtnDelCompany_Click(object sender, EventArgs e)
    {
        delImg(2);
    }
    protected void delImg(int prmType)
    {
        clsCompanyPhoto cls = new clsCompanyPhoto(ua.UserID, prmType);
        clsCompanyPhoto.Delete(cls);
        Response.Redirect("UC2UploadPhoto.aspx", true);
    }


    protected void btnSaveUpLogo_Click(object sender, EventArgs e)
    {
        clsCompanyPhoto cls = new clsCompanyPhoto();
        cls.UserID = ua.UserID;
        cls.PhotoType = 0;
        string strFilenamePath = "";
        string strFileExt = "";
        string strRootPath = Page.MapPath("../upfiles/" + cls._FileSaveDir);

        FileUpload ctlFup = fupLogo;
        if (ctlFup.HasFile)
        {
            strFileExt = System.IO.Path.GetExtension(ctlFup.FileName).ToLower();
            if (strFileExt != ".jpg" && strFileExt != ".gif" && strFileExt != ".png" && strFileExt != ".bmp")
            {
                Response.Write("<script>alert('提示：\\n\\n格式有问题，只技持jpg|gif|png|bmp格式文件哦！');history.go(-1);</script>");
                Response.End();
            }
            else
            {
                strFilenamePath = Utils.MakeRndName(strRootPath) + "_0" + strFileExt;

                ctlFup.SaveAs(strRootPath + "\\" + strFilenamePath);

                if (cls.PhotoPath != "") clsCompanyPhoto.DelFile(cls.PhotoPath);

                cls.PhotoPath = "upfiles/" + cls._FileSaveDir + "/" + strFilenamePath;

                clsCompanyPhoto.Save(cls);
            }
        }

        Response.Redirect("UC2UploadPhoto.aspx", true);
    }

    protected void btnSaveUpBanner_Click(object sender, EventArgs e)
    {
        clsCompanyPhoto cls = new clsCompanyPhoto();
        cls.UserID = ua.UserID;
        cls.PhotoType = 1;
        string strFilenamePath = "";
        string strFileExt = "";
        string strRootPath = Page.MapPath("../upfiles/" + cls._FileSaveDir);

        FileUpload ctlFup = fupBanner;
        if (ctlFup.HasFile)
        {
            strFileExt = System.IO.Path.GetExtension(ctlFup.FileName).ToLower();
            if (strFileExt != ".jpg" && strFileExt != ".gif" && strFileExt != ".png" && strFileExt != ".bmp")
            {
                Response.Write("<script>alert('提示：\\n\\n格式有问题，只技持jpg|gif|png|bmp格式文件哦！');history.go(-1);</script>");
                Response.End();
            }
            else
            {
                strFilenamePath = Utils.MakeRndName(strRootPath) + "_1" + strFileExt;

                ctlFup.SaveAs(strRootPath + "\\" + strFilenamePath);

                if (cls.PhotoPath != "") clsCompanyPhoto.DelFile(cls.PhotoPath);

                cls.PhotoPath = "upfiles/" + cls._FileSaveDir + "/" + strFilenamePath;

                clsCompanyPhoto.Save(cls);
            }
        }
        Response.Redirect("UC2UploadPhoto.aspx", true);
    }

    protected void btnSaveUpCompany_Click(object sender, EventArgs e)
    {
        clsCompanyPhoto cls = new clsCompanyPhoto();
        cls.UserID = ua.UserID;
        cls.PhotoType = 2;
        string strFilenamePath = "";
        string strFileExt = "";
        string strRootPath = Page.MapPath("../upfiles/" + cls._FileSaveDir);

        FileUpload ctlFup = fupCompany;
        if (ctlFup.HasFile)
        {
            strFileExt = System.IO.Path.GetExtension(ctlFup.FileName).ToLower();
            if (strFileExt != ".jpg" && strFileExt != ".gif" && strFileExt != ".png" && strFileExt != ".bmp")
            {
                Response.Write("<script>alert('提示：\\n\\n格式有问题，只技持jpg|gif|png|bmp格式文件哦！');history.go(-1);</script>");
                Response.End();
            }
            else
            {
                strFilenamePath = Utils.MakeRndName(strRootPath) + "_2" + strFileExt;

                ctlFup.SaveAs(strRootPath + "\\" + strFilenamePath);

                if (cls.PhotoPath != "") clsCompanyPhoto.DelFile(cls.PhotoPath);

                cls.PhotoPath = "upfiles/" + cls._FileSaveDir + "/" + strFilenamePath;

                clsCompanyPhoto.Save(cls);
            }
        }
        Response.Redirect("UC2UploadPhoto.aspx", true);
    }

}